package com.liu.communitysystemend.module.room.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.communitysystemend.exception.CommonException;
import com.liu.communitysystemend.exception.SavaException;
import com.liu.communitysystemend.module.room.domain.Room;
import com.liu.communitysystemend.module.room.domain.RoomDto;
import com.liu.communitysystemend.module.room.mapper.RoomMapper;
import com.liu.communitysystemend.module.room.service.RoomService;
import com.liu.communitysystemend.module.user.domain.User;
import com.liu.communitysystemend.module.user.service.UserService;
import com.liu.communitysystemend.utils.Result;
import com.liu.communitysystemend.utils.WStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
*
*/
@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private UserService userService;

    @Override
    public Room getUserById(String id) {
        if (id == null) {
            throw new CommonException(501, "参数为空");
        }
        LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Room::getId,id);
        Room room = roomMapper.selectOne(wrapper);
        return room;
    }

    @Override
    public Room update(Room room) {
        LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Room::getId,room.getId());
        int update = roomMapper.update(room, wrapper);
        if (update>0){
            return room;
        }
        throw new CommonException(502,"更新失败");
    }

    @Override
    public Room add(Room room) {
        if(room ==null){
            throw new SavaException("插入用户失败,菜单的类型实体为空");
        }
        int insert=roomMapper.insert(room);
        if(insert>0){
            return room;
        }
        throw new SavaException("插入用户失败");
    }

    @Override
    public Page<Room> getPage(Room room)
    {
        LambdaQueryWrapper<Room> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Room::getIsDeleted,0);
        Page<Room> page=new Page<>(room.getPage().getPage(),room.getPage().getSize());
        Page<Room> poiPage=(Page<Room>) roomMapper.selectPage(page,wrapper);
        return poiPage;
    }

    @Override
    public Integer deleteById(String id) {
        Room room = selectOneById(id);
        if(room==null){
            throw new CommonException(4004,"找不到id为:"+id+"的数据");
        }
        int i=roomMapper.deleteById(id);
        return i;
    }

    public Room selectOneById(String id){
        Room room=roomMapper.selectById(id);
        return  room;
    }

    public List<Room> getList(Room room){
        return  roomMapper.getList(room);
    }

    @Override
    public Result checkIn(RoomDto roomDto) {
        if ("".equals(roomDto.getIdCard()) || WStringUtils.isBlank(roomDto.getIdCard())){
            throw new CommonException("身份证号不能为空");
        }
        User user = userService.selectByIdCard(roomDto.getIdCard());
        if (user==null){
            throw new CommonException("此账户不存在或已被禁用");
        }
        Room room = selectOneById(roomDto.getId());
        if (room.getStatus()==1 || room==null){
            throw new CommonException("入住失败，此房已入住或房间状态异常");
        }
        Room room1 = new Room();
        BeanUtils.copyProperties(room,room1);
        room1.setUserId(user.getId());
        room1.setStatus(1);
        room1.setInTime(new Date());
        update(room1);
        user.setRoomId(room1.getId());
        userService.update(user);
        return Result.success();
    }

    @Override
    public Result checkOut(RoomDto roomDto) {
        if ("".equals(roomDto.getIdCard()) || WStringUtils.isBlank(roomDto.getIdCard())){
            throw new CommonException("身份证号不能为空或身份证号输入错误");
        }
        User user = userService.selectByIdCard(roomDto.getIdCard());
        Room room = selectOneById(roomDto.getId());
        if (room.getStatus()==0 || room==null){
            throw new CommonException("退房失败，此房未入住或房间状态异常");
        }
        Room room1 = new Room();
        BeanUtils.copyProperties(room,room1);
        room1.setUserId("-");
        room1.setStatus(0);
        room1.setInTime(null);
        update(room1);
        user.setRoomId("");
        userService.update(user);
        return Result.success();
    }

    @Override
    public Integer test() {
        for (int i = 0; i < 20; i++) {
            Room room = new Room();
            room.setId("100"+i);
            room.setInTime(new Date());
            room.setStatus(1);
            room.setUserId("100"+i);
            room.setBuilding("1栋");
            room.setRoomSize(150);
            room.setWater(100);
            room.setElectricity(100);
            room.setManagementFee(100);
            room.setFloor(i/5+1+"楼");
            room.setName(i/5+1+"0"+i/5+1);
            roomMapper.insert(room);
        }
        return 1;
    }

    @Override
    public User CheckInfo(String id) {
       if (id==null || id==""){
          throw new CommonException(501,"房间id不能为空");
       }
        User user = userService.selectByRoomId(id);
        if (user!=null){
            return user;
        }
        throw new CommonException(501,"该房间未入住");
    }
}
